home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9421 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: solon.com!not-for-mail
  2. From: tanmoy@qcd.Lanl.GOV (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Integral promotion problem
  5. Date: 10 Mar 1996 10:32:14 -0600
  6. Organization: Los Alamos National Laboratory
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4hv06e$jb9@solutions.solon.com>
  10. References: <4hqedq$1mj@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13. In article <4hqedq$1mj@solutions.solon.com>
  14. johnb@pivotal-dm.ccmail.compuserve.com (John Bain) writes:
  15.  
  16. JB: Hi,
  17. JB: 
  18. JB: I thought I understood integral promotion, but the following example has
  19. JB: me puzzled.
  20. <snip>
  21. JB: unsigned short s = 0xFFFF;
  22. <snip>
  23. JB:     int i = ~s;
  24. JB: 
  25. JB:     printf ("%x %x\n", i, (int)~s);
  26. <snip>
  27. JB:     if ((int)~s)
  28. JB:         printf("(int)~s - NonZero\n");
  29. JB:     else
  30. JB:         printf("(int)~s - Zero\n");
  31. <snip>
  32. JB: When compiled on an implementation with 32-bit 2's complement ints I get
  33. JB: the following output:
  34. JB: 
  35. JB: -------------------
  36. JB: ffff0000 ffff0000
  37. JB: i - NonZero
  38. JB: (int)~s - Zero
  39. JB: ------------------
  40. JB: 
  41. JB: The hex values and the result of the first "if" are the "surprising"
  42. JB: results I was expecting.  However, the result of the second "if" has me
  43. JB: confused.
  44. JB: 
  45. JB: What am I missing?
  46. JB: 
  47.  
  48. Compiler error? 
  49.  
  50. JB: (In case it's relevent, the implementation defines the conversion of an
  51. JB: unsigned integer to a signed integer, when the unsigned value cannot be
  52. JB: represented in the signed integer, as being performed by truncation of
  53. JB: the most significant bits if the signed integer is shorter, and then
  54. JB: interpreting the remaining bits as a 2's complement integer.)
  55.  
  56. Why should that be relevant? What am I missing?
  57.  
  58. Cheers
  59. Tanmoy
  60. --
  61. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  62. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  63. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  64. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  65. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  66. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  67.